package com.klouda.gboff;

import com.klouda.gboff.db.Tables;

import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ImageCursorAdapter extends SimpleCursorAdapter {

  private Cursor c;
  private Context context;

  public ImageCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.c = c;
    this.context = context;
  }

  public View getView(int pos, View inView, ViewGroup parent) {
    View v = inView;
    if (v == null) {
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      v = inflater.inflate(R.layout.tablerow, null);
    }
    this.c.moveToPosition(pos);
    String l_bookName = this.c.getString(this.c.getColumnIndex(Tables.TBooks.COLUMN_NAME_NAME));
    String l_authors = this.c.getString(this.c.getColumnIndex(Tables.TBooks.COLUMN_NAME_AUTHORS));
    byte[] l_thumbnail = this.c.getBlob(this.c.getColumnIndex(Tables.TBooks.COLUMN_NAME_THUMBNAIL));
    ImageView iv = (ImageView) v.findViewById(R.id.small_thumbnail);
    if (l_thumbnail != null) {
      iv.setImageBitmap(BitmapFactory.decodeByteArray(l_thumbnail, 0, l_thumbnail.length));
    } else {
      iv.setImageDrawable(null);;
    }
    TextView l_textViewName = (TextView) v.findViewById(R.id.txtName);
    l_textViewName.setText(l_bookName);

    TextView l_textViewAuthors = (TextView) v.findViewById(R.id.txtAuthors);
    l_textViewAuthors.setText(l_authors);

    return (v);
  }
}